package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.UserFollow;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/userFollow")

public class UserFollowController extends BaseController {

     @Reference
    private UserFollowService  userFollowService;

    @GetMapping("/auth/follow/{houseId}")
    public Result follow(@PathVariable  Long houseId, HttpServletRequest request){
        UserFollow userFollow =new UserFollow();
        userFollow.setHouseId(houseId);

        //从session中去取userId
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        userFollow.setUserId(userInfo.getId());

        //调用业务
        userFollowService.insert(userFollow);


        return  Result.ok();
    }


    @RequestMapping("/auth/list/{pageNum}/{pageSize}")
     public  Result list(@PathVariable Integer pageNum,@PathVariable Integer pageSize,HttpServletRequest request){

        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        Long userId = userInfo.getId();
       PageInfo<UserFollowVo> page= userFollowService.findListPage(pageNum,pageSize,userId);


        return  Result.ok(page);


     }
    @RequestMapping("/auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable Long id){
        this.userFollowService.cancelFollow(id);
        return Result.ok();
    }
}
